/**
 * Created by fly on 2017/4/20.
 * 关于装修公司相关逻辑处理
 */
var db = require('../db/connect_pool');
var Q = require('q');

/**
 * 执行sql语句promise
 * @param sql
 * @param value
 * @returns {*|promise}
 */
//todo 应该写到一个单独的文件中
function doSqlPromise (sql,value) {
    var deferred = Q.defer();
    db.query(sql,value,function (result) {
        deferred.resolve(result);
    });
    return deferred.promise;
}
/**
 * 获取装修公司信息
 * @param data 筛选装修公司的条件  json格式
 * @param callback
 */
exports.getFitCompany = function (data, callback) {
    var begin = data.begin || 0;
    var back = {};
    //todo 筛选类型和价位需要完善 数据库需要添加字段
    doSqlPromise('select * from z_fitment_company limit ?,10',[begin])
        .then(function (result) {
            back.company = result
        })
        .then(function () {
            doSqlPromise('select count(*) as pages from z_fitment_company')
                .then(function (result) {
                    back.pages = result[0].pages
                })
                .then(function () {
                    callback(back)
                });
        });
}
/**
 * 筛选装修公司
 * @param data 筛选条件
 * @returns {*|promise}
 */
exports.filterCompany = function (data) {
    var deferred = Q.defer();
    var filter = data.select;
    var page = data.page;
    var style='',price=0;
    var back = {},sql = '',sqlpage = '';
    if(filter[0]){
        style = filter[0].split('.')[1]
    }
    if(filter[1]){
        price = filter[1]*10000
    }
    // console.log(style,price,page)
    if(style == ''){
         sql = 'select * from z_fitment_company' +
            ' left join company_good_price on' +
            '(z_fitment_company.id = company_good_price.companyid) ' +
            'where company_good_price.maxprice > ?  limit ?,10';
        sqlpage = 'select count(*) as pages from z_fitment_company' +
            ' left join company_good_price on' +
            '(z_fitment_company.id = company_good_price.companyid) ' +
            'where company_good_price.maxprice > ?';
        doSqlPromise(sql, [price,page])
            .then(function (result) {
                back.company = result;
            })
            .then(function () {
                doSqlPromise(sqlpage, [price])
                    .then(function (result) {
                        back.pages = result[0].pages;
                    })
                    .then(function () {
                        deferred.resolve(back);
                    });
            });
    }else{
        sql = 'select * from z_fitment_company left join company_good_style\
            on(z_fitment_company.id = company_good_style.companyid)\
            left join company_good_price on\
            (z_fitment_company.id = company_good_price.companyid)\
             where styleid = ? and maxprice > ? limit ?,10';
        sqlpage = 'select count(*) as pages from z_fitment_company \
            left join company_good_style\
             on(z_fitment_company.id = company_good_style.companyid)\
             left join company_good_price on\
            (z_fitment_company.id = company_good_price.companyid)\
             where styleid = ? and maxprice > ?';
        doSqlPromise(sql, [style,price,page])
            .then(function (result) {
                back.company = result;
            })
            .then(function () {
                doSqlPromise(sqlpage, [style,price])
                    .then(function (result) {
                        back.pages = result[0].pages
                    })
                    .then(function () {
                        deferred.resolve(back);
                    });
            });
    }
    return deferred.promise;
}

/**
 * 获取商品
 * @param data
 * @param callback
 */
exports.getMallGoods = function (data, callback) {
    var data = {};
    doSqlPromise('select fitment_goods.*,goods_style.name as goodsname from fitment_goods left join goods_style on(goods_style.id = fitment_goods.goodstype) where goodstype = 1 limit 0,6',[])
        .then(function (result) {
            data.type1 = result;
        })
        .then(function () {
            doSqlPromise('select fitment_goods.*,goods_style.name as goodsname from fitment_goods left join goods_style on(goods_style.id = fitment_goods.goodstype) where goodstype = 2 limit 0,6',[])
                .then(function (results) {
                    data.type2 = results;
                    callback({'data':data});
                });
        });
}
/**
 * 获取装修分类
 * @returns {*}
 */
exports.getCompanyStylePrice = function () {
    var deferred = Q.defer();
    doSqlPromise('select * from fitment_style')
        .then(function (result) {
            deferred.resolve(result);
        });
    return deferred.promise;
}
